package algo.dijkstra;

import algo.Edge;
import algo.Graph;
import algo.Node;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author robin
 */
public class Transformer {

    /**
     * Static method to transform the 2 dimensional road array into a graph with
     * nodes and edges
     *
     * @param road
     * @return
     */
    public static Graph transformToGraph(int[][] road) {
        List<Node> nodes = new ArrayList<Node>();
        List<Edge> edges = new ArrayList<Edge>();
        int distance = 10;

        for (int i = 0; i < road.length; i++) {
            Node node = new Node(i);

            if (road[i][0] == 0) {
                addEdges(edges, i, i - 20, distance);
            }
            if (road[i][1] == 0) {
                addEdges(edges, i, i + 1, distance);
            }
            if (road[i][2] == 0) {
                addEdges(edges, i, i + 20, distance);
            }
            if (road[i][3] == 0) {
                addEdges(edges, i, i - 1, distance);
            }
            nodes.add(node);
        }

        Graph graph = new Graph(nodes, edges);
        return graph;
    }

    public static int[][][] transformTo2DArray(int[][] road) {
        int[][][] graph = new int[20][20][4];
        int roadcounter = 0;

        for (int i = 0; i < graph.length; i++) {
            for (int j = 0; j < graph[i].length; j++) {
                graph[j][i] = road[roadcounter];
                roadcounter++;
            }
        }
        return graph;
    }

    public static int[] transformGraphPointTo2D(int block) {
        int x = -1;
        int y = 0;

        while (block >= 0) {
            x++;

            if (x == 20) {
                x = 0;
                y++;
            }
            block--;
        }
        int[] couple = {x, y};
        return couple;
    }

    public static int transformCoupleToBlock(int[] couple) {
        int block = 0;
        int x = couple[0];
        int y = couple[1];

        if (y != 0) {
            block += (y * 20);
        }
        block += x;
        return block;
    }

    private static void addEdges(List<Edge> edges, int src, int dst, int distance) {
        edges.add(new Edge(new Node(src), new Node(dst), distance));
    }
}